/*
 * Blue Pumpkin
 */
package vn.hva.bluepumpkin.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import vn.hva.bluepumpkin.Constants;

/**
 *
 * @author Lang Van Hao
 */
public class SendMail {

    public static boolean send(String to, String username, String pass, String fullname) throws Exception {
        // java.security.Security.addProvider(new  com.sun.net.ssl.internal.ssl.Provider());
        String smtpServer = "smtp.gmail.com";
        String port = "587";
        String from = "bluepumpkin01@gmail.com";
        String pw = "adminbluepumpkin";
        try {
            if(to.contains("@gmail")){
                    smtpServer = "smtp.gmail.com";
                    port = "587";
                }else if(to.contains("@yahoo")){
                    smtpServer = "smtp.mail.yahoo.com";
                    port = "25";
//                    from = "";
//                    pw = "";
                }
            Properties props = System.getProperties();

            props.put("mail.smtp.host", smtpServer);
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.starttls.enable", "true");

            
            String subject = "BluePumpkin: Your password";
            String body = "Hello <strong>" + fullname + "</strong>,<br/><br/>" +
                    "This is your account information " +
                    "<br/>Username: <strong>" + username +
                    "</strong><br/>Passowrd: <strong>" + pass +
                    "</strong><br/>You can visit " + Constants.EMPLOYEE_URL + "login.jsp" +
                    "<br/><br/>Thanks and Regards, ";
            final String login = from;
            final String pwd = pw;

            Authenticator pa = null;
            if (login != null && pwd != null) {
                //authentication required?
                props.put("mail.smtp.auth", "true");
                pa = new Authenticator() {

                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(login, pwd);
                    }
                };
            }//else: no authentication
            Session session = Session.getInstance(props, pa);

            // Create a new message
            Message msg = new MimeMessage(session);

            // Set the FROM and TO fields
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
                    to, false));

            // Set the subject and body text
            msg.setSubject(subject);
            msg.setContent(body, "text/html; charset=UTF-8");

            // Set some other header information
            msg.setHeader("X-Mailer", "LOTONtechEmail");
            msg.setSentDate(new Date());
            msg.saveChanges();

            Transport.send(msg);
            System.out.println("Message sent OK!");
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
